/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：User_recharge                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： User_rechargeService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace NetCoreCMS.Services
{
    public class User_rechargeService: IUser_rechargeService
    {
        private readonly IUser_rechargeRepository _repository;
        private readonly IUser_amount_logRepository _user_amount_log;

        public User_rechargeService(IUser_rechargeRepository repository, IUser_amount_logRepository user_amount_log)
        {
            _repository = repository;
            _user_amount_log = user_amount_log;
        }

        public int Add(int user_id, string user_name, string recharge_no, int payment_id, decimal amount)
        {
            Models.User_recharge model = new Models.User_recharge();
            model.user_id = user_id;
            model.user_name = user_name;
            model.recharge_no = recharge_no;
            model.payment_id = payment_id;
            model.amount = amount;
            model.status = 0;
            return (int)_repository.Insert(model);
        }

        public bool Confirm(string recharge_no)
        {
            using (TransactionScope scope=new TransactionScope())
            {
                Models.User_recharge model = GetModel(recharge_no);//根据充值单号得到实体
                if (model == null)
                {
                    return false;
                }
                #region 增加一条账户余额记录===============
                Models.User_amount_log amountModel = new Models.User_amount_log();
                amountModel.user_id = model.user_id;
                amountModel.user_name = model.user_name;
                amountModel.value = model.amount;
                amountModel.remark = "在线充值，单号：" + recharge_no;
                amountModel.add_time = DateTime.Now;
                _user_amount_log.Add(amountModel);
                #endregion

                #region 更新充值表=========================
                model.status = 1;
                model.complete_time = DateTime.Now;
                _repository.Update(model);
                #endregion

                scope.Complete();
                return true;
            }
        }

        public bool Delete(int id)
        {
            return _repository.Delete(id) > 0;
        }

        public bool Delete(int id, string user_name)
        {
            return _repository.DeleteList("where id=@id and user_name=@user_name",new { id=id,user_name=user_name}) > 0;
        }

        public List<Models.User_recharge> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }

        public User_recharge GetModel(string recharge_no)
        {
            return _repository.GetList("where recharge_no=@recharge_no", new { recharge_no = recharge_no }).FirstOrDefault();
        }

        public bool Recharge(User_recharge model)
        {
            return _repository.Recharge(model);
        }

        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }
    }
}